package cn.edu.sdjzu.xg.bysj.service;
//201902104061徐中堂
import cn.edu.sdjzu.xg.bysj.dao.DepartmentDao;
import cn.edu.sdjzu.xg.bysj.dao.SchoolDao;
import cn.edu.sdjzu.xg.bysj.domain.School;
import exception.BysjException;
import util.Condition;
import util.JdbcHelper;
import util.Pagination;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.Collection;

public final class SchoolService{
	//获取SchoolDao实例
	private static SchoolDao schoolDao= SchoolDao.getInstance();
	//新建SchoolService对象
	private static SchoolService schoolService=new SchoolService();
	//获取SchoolService对象
	public static SchoolService getInstance(){
		return schoolService;
	}

	/**
	 * 向schoolDao发消息根据条件和分页查找
	 * @param condition
	 * @param pagination
	 * @return
	 * @throws SQLException
	 */
	public Collection<School> findAll(Collection<Condition> condition, Pagination pagination) throws SQLException {
		//获取数据库连接
		Connection connection = JdbcHelper.getConn();
		Collection<School> schools = null;
		//关闭自动提交，事务开始
		connection.setAutoCommit(false);
		try {
			schools = schoolDao.findAll(connection,condition,pagination);
			//提交事务
			connection.commit();
		}
		catch (SQLException e) {
			//回滚事务中所有操作
			connection.rollback();
		}
		finally {
			//将自动提交设置为true，结束事务
			connection.setAutoCommit(true);
			//释放连接
			JdbcHelper.close(connection);
		}
		return schools;
	}

	/**
	 * 向schoolDao发消息根据id查找
	 * @param id
	 * @return
	 * @throws SQLException
	 */
	public School find(Integer id) throws SQLException {
		//获取数据库连接
		Connection connection = JdbcHelper.getConn();
		School school = null;
		//关闭自动提交，事务开始
		connection.setAutoCommit(false);
		try {
			school = schoolDao.find(connection,id);
			//提交事务
			connection.commit();
		}
		catch (SQLException e) {
			//回滚事务中所有操作
			connection.rollback();
		}
		finally {
			//将自动提交设置为true，结束事务
			connection.setAutoCommit(true);
			//释放连接
			JdbcHelper.close(connection);
		}
		return school;
	}

	/**
	 * 向schoolDao发消息更新
	 * @param school
	 * @return
	 * @throws SQLException
	 */
	public boolean update(School school) throws SQLException {
		//获取数据库连接
		Connection connection = JdbcHelper.getConn();
		boolean updated = false;
		//关闭自动提交，事务开始
		connection.setAutoCommit(false);
		try {
			updated = schoolDao.update(connection,school);
			//提交事务
			connection.commit();
		}
		catch (SQLException e) {
			//回滚事务中所有操作
			connection.rollback();
		}
		finally {
			//将自动提交设置为true，结束事务
			connection.setAutoCommit(true);
			//释放连接
			JdbcHelper.close(connection);
		}
		return updated;
	}

	/**
	 * 向schoolDao发消息添加
	 * @param school
	 * @return
	 * @throws SQLException
	 */
	public boolean add(School school) throws SQLException {
		//获取数据库连接
		Connection connection = JdbcHelper.getConn();
		boolean added = false;
		//关闭自动提交，事务开始
		connection.setAutoCommit(false);
		try {
			added = schoolDao.add(connection,school);
			//提交事务
			connection.commit();
		}
		catch (SQLException e) {
			//回滚事务中所有操作
			connection.rollback();
		}
		finally {
			//将自动提交设置为true，结束事务
			connection.setAutoCommit(true);
			//释放连接
			JdbcHelper.close(connection);
		}
		return added;
	}

	/**
	 * 向schoolDao发消息删除，如果没有下属学院则删除成功
	 * @param id
	 * @return
	 * @throws SQLException
	 * @throws BysjException
	 */
	public boolean delete(Integer id) throws SQLException, BysjException {
		//获取数据库连接
		Connection connection = JdbcHelper.getConn();
		School school = schoolDao.find(connection,id);
		//查找学院是否有下属系
		int countDepartmentOfSchool = DepartmentDao.getInstance().countAll(connection,school);
		//如果没有则可以删除
		if (countDepartmentOfSchool == 0) {
			boolean deleted = schoolDao.delete(connection,id);
			JdbcHelper.close(connection);
			return deleted;
		}else {
			//否则抛出异常
			throw new BysjException(school.getDescription() + "仍有下属，无法删除");
		}
	}

}
